package cn.zhangjie.tran;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

public class Producer {


    public static void main(String[] args) throws Exception {
        //1.创建生产者对象，指定生产者组名
        TransactionMQProducer Producer = new TransactionMQProducer("syn-producer-group");
        //2.连接mq
        Producer.setNamesrvAddr("localhost:9876");
        //3.启动
        //3.1设置
        Producer.setTransactionListener(new MyTranListener());
        Producer.start();
        //4.创建消息，设置消息对象
        Message message = new Message("sync", "hello", "我要回家".getBytes(StandardCharsets.UTF_8));
        SendResult send = Producer.sendMessageInTransaction(message,"事务消息");
        System.out.println(send);
        //5.关闭
        Producer.shutdown();

    }
}
